home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1989 …il & Dave's Excellent CD / Excellent CD HFS.raw / Moof / Goodies / DTS Goodies / IM's Hacks / TCP PBControl / MacTCP PBControl.c
Encoding:
C/C++ Source or Header  |  1989-04-12  |  11.7 KB  |  431 lines  |  [TEXT/MPS ]

  1. /*
  2. ******************************** Header Files
  3. */
  4.  
  5. #include    "MacTCPCommonTypes.h"
  6. #include    "TCPPB.h"
  7. #include    "AddressXlation.h"
  8. #include    "GetMyIPAddr.h"
  9.  
  10. /*
  11. ******************************** Global IPP Driver Reference Number
  12. */
  13.  
  14. short        gIPPDriverRefNum;
  15.  
  16. /*
  17. ******************************** PBControl Interface Prototypes
  18. */
  19.  
  20. OSErr _TCPInit            (void);
  21. OSErr _TCPGetIP            (struct IPParamBlock *pb, ip_addr *ip, long *netMask, ProcPtr ioCompletion, Boolean async);
  22. OSErr _TCPCreate        (TCPiopb *pb, StreamPtr *stream, Ptr rcvBufPtr, long rcvBufLen, TCPNotifyProc aNotifyProc, Ptr userDataPtr, TCPIOCompletionProc ioCompletion, Boolean async);
  23. OSErr _TCPPassiveOpen    (TCPiopb *pb, StreamPtr stream, ip_addr *remoteIP, tcp_port *remotePort, ip_addr *localIP, tcp_port *localPort, Ptr userData, TCPIOCompletionProc ioCompletion, Boolean async);
  24. OSErr _TCPActiveOpen    (TCPiopb *pb, StreamPtr stream, ip_addr remoteIP, tcp_port remotePort, ip_addr *localIP, tcp_port *localPort, Ptr userData, TCPIOCompletionProc ioCompletion, Boolean async);
  25. OSErr _TCPSend            (TCPiopb *pb, StreamPtr stream, wdsEntry *wdsPtr, Ptr userData, TCPIOCompletionProc ioCompletion, Boolean async);
  26. OSErr _TCPNoCopyRcv        (TCPiopb *pb, StreamPtr stream, rdsEntry *rdsPtr, unsigned short rdsLength, Ptr userData, TCPIOCompletionProc ioCompletion, Boolean async);
  27. OSErr _TCPRcv            (TCPiopb *pb, StreamPtr stream, Ptr rcvBufPtr, unsigned short *rcvBufLen, Ptr userData, TCPIOCompletionProc ioCompletion, Boolean async);
  28. OSErr _TCPBfrReturn        (TCPiopb *pb, StreamPtr stream, Ptr rdsPtr, Ptr userData, TCPIOCompletionProc ioCompletion, Boolean async);
  29. OSErr _TCPClose            (TCPiopb *pb, StreamPtr stream, Ptr userData, TCPIOCompletionProc ioCompletion, Boolean async);
  30. OSErr _TCPAbort            (TCPiopb *pb, StreamPtr stream, Ptr userData, TCPIOCompletionProc ioCompletion, Boolean async);
  31. OSErr _TCPStatus        (TCPiopb *pb, StreamPtr stream, struct TCPStatusPB *status, Ptr userData, TCPIOCompletionProc ioCompletion, Boolean async);
  32. OSErr _TCPRelease        (TCPiopb *pb, StreamPtr stream, Ptr *rcvBufPtr, long *rcvBufLen, TCPIOCompletionProc ioCompletion, Boolean async);
  33.  
  34. /*
  35. ******************************** PBControl Interface Procedures
  36. */
  37.  
  38. #pragma segment TCPPB
  39.  
  40. OSErr    _TCPInit()
  41. {
  42.  
  43.     auto    ParamBlockRec        pb;
  44.     auto    OSErr                osErr;
  45.         
  46.     pb.ioParam.ioCompletion        = 0; 
  47.     pb.ioParam.ioNamePtr        = "\p.ipp"; 
  48.     pb.ioParam.ioPermssn        = fsCurPerm;
  49.     
  50.     osErr = PBOpen(&pb, false);
  51.     if (noErr == osErr) {
  52.         gIPPDriverRefNum        = pb.ioParam.ioRefNum;
  53.     }
  54.         
  55.     return osErr;
  56. }
  57.  
  58. OSErr    _TCPGetIP(pb, ip, netMask, ioCompletion, async)
  59.             struct IPParamBlock        *pb;
  60.             ip_addr                    *ip;
  61.             long                    *netMask;
  62.             ProcPtr                    ioCompletion;
  63.             Boolean                    async;
  64. {
  65.  
  66.     auto    OSErr                    osErr;
  67.     
  68.     pb->ioCompletion                = ioCompletion;
  69.     pb->ioCRefNum                    = gIPPDriverRefNum;
  70.     pb->csCode                        = ipctlGetAddr;
  71.         
  72.     osErr = PBControl((ParmBlkPtr) pb, async);
  73.     if (!async && (noErr == osErr)) {
  74.         *ip                            = pb->ourAddress;
  75.         *netMask                    = pb->ourNetMask;
  76.     }
  77.         
  78.     return osErr;
  79. }
  80.  
  81.  
  82. OSErr    _TCPCreate(pb, stream, rcvBufPtr, rcvBufLen, aNotifyProc, userDataPtr, ioCompletion, async)
  83.             TCPiopb                    *pb;
  84.             StreamPtr                *stream;
  85.             Ptr                        rcvBufPtr;
  86.             long                    rcvBufLen;
  87.             TCPNotifyProc            aNotifyProc;
  88.             Ptr                        userDataPtr;
  89.             TCPIOCompletionProc        ioCompletion;
  90.             Boolean                    async;
  91. {
  92.  
  93.     auto    OSErr                    osErr;
  94.     
  95.     pb->ioCompletion                    = ioCompletion;
  96.     pb->ioCRefNum                        = gIPPDriverRefNum;
  97.     pb->csCode                            = TCPCreate;
  98.     
  99.     pb->csParam.create.rcvBuff            = rcvBufPtr;    
  100.     pb->csParam.create.rcvBuffLen        = rcvBufLen;    
  101.     pb->csParam.create.notifyProc        = aNotifyProc;
  102.     pb->csParam.create.userDataPtr        = userDataPtr;
  103.     
  104.     osErr = PBControl((ParmBlkPtr) pb, async);
  105.     if (!async && (noErr == osErr)) {
  106.         *stream                            = pb->tcpStream;
  107.     }
  108.         
  109.     return osErr;
  110. }
  111.  
  112.  
  113. OSErr _TCPPassiveOpen (pb, stream, remoteIP, remotePort, localIP, localPort, userData, ioCompletion, async)
  114.             TCPiopb                    *pb;
  115.             StreamPtr                stream;
  116.             ip_addr                    *remoteIP; 
  117.             tcp_port                *remotePort; 
  118.             ip_addr                    *localIP; 
  119.             tcp_port                *localPort;
  120.             Ptr                        userData;
  121.             TCPIOCompletionProc        ioCompletion;
  122.             Boolean                    async;
  123. {
  124.  
  125.     auto    OSErr                    osErr;
  126.     auto    short                    index;
  127.     
  128.     pb->ioCompletion                        = ioCompletion;
  129.     pb->ioCRefNum                            = gIPPDriverRefNum;
  130.     pb->csCode                                = TCPPassiveOpen;
  131.     pb->tcpStream                            = stream;
  132.     
  133.     pb->csParam.open.ulpTimeoutValue        = 0;    
  134.     pb->csParam.open.ulpTimeoutAction        = 0;    
  135.     pb->csParam.open.validityFlags            = 0;    
  136.     pb->csParam.open.commandTimeoutValue    = 0;    
  137.     pb->csParam.open.remoteHost                = *remoteIP;    
  138.     pb->csParam.open.remotePort                = *remotePort;    
  139.     pb->csParam.open.localHost                = 0;    
  140.     pb->csParam.open.localPort                = *localPort;    
  141.     pb->csParam.open.tosFlags                = 0;    
  142.     pb->csParam.open.precedence                = 0;    
  143.     pb->csParam.open.dontFrag                = 0;    
  144.     pb->csParam.open.timeToLive                = 0;
  145.     pb->csParam.open.security                = 0;    
  146.     pb->csParam.open.optionCnt                = 0;
  147.     for (index = 0; index < sizeof(pb->csParam.open.options); ++index)    
  148.         pb->csParam.open.options[index]        = 0;    
  149.     pb->csParam.open.userDataPtr            = userData;
  150.     
  151.     osErr = PBControl((ParmBlkPtr) pb, async);
  152.     if (!async && (noErr == osErr)) {
  153.         *remoteIP                            = pb->csParam.open.remoteHost;    
  154.         *remotePort                            = pb->csParam.open.remotePort;    
  155.         *localIP                            = pb->csParam.open.localHost;    
  156.         *localPort                            = pb->csParam.open.localPort;
  157.     }
  158.  
  159.     return osErr;
  160. }
  161.  
  162.  
  163. OSErr _TCPActiveOpen (pb, stream, remoteIP, remotePort, localIP, localPort, userData, ioCompletion, async)
  164.             TCPiopb                    *pb;
  165.             StreamPtr                stream;
  166.             ip_addr                    remoteIP; 
  167.             tcp_port                remotePort; 
  168.             ip_addr                    *localIP; 
  169.             tcp_port                *localPort;
  170.             Ptr                        userData;
  171.             TCPIOCompletionProc        ioCompletion;
  172.             Boolean                    async;
  173. {
  174.  
  175.     auto    OSErr                    osErr;
  176.     auto    short                    index;
  177.     
  178.     pb->ioCompletion                        = ioCompletion;
  179.     pb->ioCRefNum                            = gIPPDriverRefNum;
  180.     pb->csCode                                = TCPActiveOpen;
  181.     pb->tcpStream                            = stream;
  182.     
  183.     pb->csParam.open.ulpTimeoutValue        = 0;
  184.     pb->csParam.open.ulpTimeoutAction        = 0;
  185.     pb->csParam.open.validityFlags            = 0;    
  186.     pb->csParam.open.commandTimeoutValue    = 0;    
  187.     pb->csParam.open.remoteHost                = remoteIP;    
  188.     pb->csParam.open.remotePort                = remotePort;    
  189.     pb->csParam.open.localHost                = 0;    
  190.     pb->csParam.open.localPort                = *localPort;    
  191.     pb->csParam.open.tosFlags                = 0;    
  192.     pb->csParam.open.precedence                = 0;    
  193.     pb->csParam.open.dontFrag                = 0;    
  194.     pb->csParam.open.timeToLive                = 0;
  195.     pb->csParam.open.security                = 0;    
  196.     pb->csParam.open.optionCnt                = 0;
  197.     for (index = 0; index < sizeof(pb->csParam.open.options); ++index)    
  198.         pb->csParam.open.options[index]        = 0;    
  199.     pb->csParam.open.userDataPtr            = userData;
  200.     
  201.     osErr = PBControl((ParmBlkPtr) pb, async);
  202.     if (!async && (noErr == osErr)) {
  203.         *localIP                            = pb->csParam.open.localHost;    
  204.         *localPort                            = pb->csParam.open.localPort;
  205.     }
  206.  
  207.     return osErr;
  208. }
  209.  
  210.  
  211. OSErr _TCPSend (pb, stream, wdsPtr, userData, ioCompletion, async)
  212.             TCPiopb                    *pb;
  213.             StreamPtr                stream;
  214.             wdsEntry                 *wdsPtr;
  215.             Ptr                        userData;
  216.             TCPIOCompletionProc        ioCompletion;
  217.             Boolean                    async;
  218. {
  219.  
  220.     auto    OSErr                    osErr;
  221.     
  222.     pb->ioCompletion                    = ioCompletion;
  223.     pb->ioCRefNum                        = gIPPDriverRefNum;
  224.     pb->csCode                            = TCPSend;
  225.     pb->tcpStream                        = stream;
  226.     
  227.     pb->csParam.send.ulpTimeoutValue    = 0;    
  228.     pb->csParam.send.ulpTimeoutAction    = 0;    
  229.     pb->csParam.send.validityFlags        = 0;    
  230.     pb->csParam.send.pushFlag            = 0;    
  231.     pb->csParam.send.urgentFlag            = 0;    
  232.     pb->csParam.send.wdsPtr                = (Ptr) wdsPtr;    
  233.     pb->csParam.send.sendFree            = 0;    
  234.     pb->csParam.send.sendLength            = 0;    
  235.     pb->csParam.send.userDataPtr        = userData;
  236.     
  237.     osErr = PBControl((ParmBlkPtr) pb, async);
  238.  
  239.     return osErr;
  240. }
  241.  
  242.  
  243. OSErr _TCPNoCopyRcv (pb, stream, rdsPtr, rdsLength, userData, ioCompletion, async)
  244.             TCPiopb                    *pb;
  245.             StreamPtr                stream;
  246.             rdsEntry                *rdsPtr;
  247.             unsigned short            rdsLength;
  248.             Ptr                        userData;
  249.             TCPIOCompletionProc        ioCompletion;
  250.             Boolean                    async;
  251. {
  252.  
  253.     auto    OSErr                    osErr;
  254.     
  255.     pb->ioCompletion                            = ioCompletion;
  256.     pb->ioCRefNum                                = gIPPDriverRefNum;
  257.     pb->csCode                                    = TCPNoCopyRcv;
  258.     pb->tcpStream                                = stream;
  259.     
  260.     pb->csParam.receive.commandTimeoutValue        = 0;    
  261.     pb->csParam.receive.urgentFlag                = 0;    
  262.     pb->csParam.receive.markFlag                = 0;    
  263.     pb->csParam.receive.rdsPtr                    = (Ptr) rdsPtr;    
  264.     pb->csParam.receive.rdsLength                = rdsLength;    
  265.     pb->csParam.receive.userDataPtr                = userData;
  266.     
  267.     osErr = PBControl((ParmBlkPtr) pb, async);
  268.  
  269.     return osErr;
  270. }
  271.  
  272.  
  273. OSErr _TCPRcv (pb, stream, rcvBufPtr, rcvBufLen, userData, ioCompletion, async)
  274.             TCPiopb                    *pb;
  275.             StreamPtr                stream;
  276.             Ptr                        rcvBufPtr;
  277.             unsigned short            *rcvBufLen;
  278.             Ptr                        userData;
  279.             TCPIOCompletionProc        ioCompletion;
  280.             Boolean                    async;
  281. {
  282.  
  283.     auto    OSErr                    osErr;
  284.     
  285.     pb->ioCompletion                            = ioCompletion;
  286.     pb->ioCRefNum                                = gIPPDriverRefNum;
  287.     pb->csCode                                    = TCPRcv;
  288.     pb->tcpStream                                = stream;
  289.     
  290.     pb->csParam.receive.commandTimeoutValue        = 0;    
  291.     pb->csParam.receive.urgentFlag                = 0;    
  292.     pb->csParam.receive.markFlag                = 0;    
  293.     pb->csParam.receive.rcvBuff                    = rcvBufPtr;    
  294.     pb->csParam.receive.rcvBuffLen                = *rcvBufLen;    
  295.     pb->csParam.receive.userDataPtr                = userData;
  296.     
  297.     osErr = PBControl((ParmBlkPtr) pb, async);
  298.     if (! async)
  299.         *rcvBufLen = pb->csParam.receive.rcvBuffLen;
  300.  
  301.     return osErr;
  302. }
  303.  
  304.  
  305. OSErr _TCPBfrReturn (pb, stream, rdsPtr, userData, ioCompletion, async)
  306.             TCPiopb                    *pb;
  307.             StreamPtr                stream;
  308.             Ptr                        rdsPtr;
  309.             Ptr                        userData;
  310.             TCPIOCompletionProc        ioCompletion;
  311.             Boolean                    async;
  312. {
  313.  
  314.     auto    OSErr                    osErr;
  315.     
  316.     pb->ioCompletion                    = ioCompletion;
  317.     pb->ioCRefNum                        = gIPPDriverRefNum;
  318.     pb->csCode                            = TCPRcvBfrReturn;
  319.     pb->tcpStream                        = stream;
  320.     
  321.     pb->csParam.receive.rdsPtr            = rdsPtr;    
  322.     pb->csParam.receive.userDataPtr        = userData;
  323.     
  324.     osErr = PBControl((ParmBlkPtr) pb, async);
  325.  
  326.     return osErr;
  327. }
  328.  
  329.  
  330. OSErr _TCPClose (pb, stream, userData, ioCompletion, async)
  331.             TCPiopb                    *pb;
  332.             StreamPtr                stream;
  333.             Ptr                        userData;
  334.             TCPIOCompletionProc        ioCompletion;
  335.             Boolean                    async;
  336. {
  337.  
  338.     auto    OSErr                    osErr;
  339.     
  340.     pb->ioCompletion                    = ioCompletion;
  341.     pb->ioCRefNum                        = gIPPDriverRefNum;
  342.     pb->csCode                            = TCPClose;
  343.     pb->tcpStream                        = stream;
  344.     
  345.     pb->csParam.close.ulpTimeoutValue    = 0;    
  346.     pb->csParam.close.ulpTimeoutAction    = 0;    
  347.     pb->csParam.close.validityFlags        = 0;    
  348.     pb->csParam.close.userDataPtr        = userData;
  349.     
  350.     osErr = PBControl((ParmBlkPtr) pb, async);
  351.  
  352.     return osErr;
  353. }
  354.  
  355.  
  356. OSErr _TCPAbort (pb, stream, userData, ioCompletion, async)
  357.             TCPiopb                    *pb;
  358.             StreamPtr                stream;
  359.             Ptr                        userData;
  360.             TCPIOCompletionProc        ioCompletion;
  361.             Boolean                    async;
  362. {
  363.  
  364.     auto    OSErr                    osErr;
  365.     
  366.     pb->ioCompletion                    = ioCompletion;
  367.     pb->ioCRefNum                        = gIPPDriverRefNum;
  368.     pb->csCode                            = TCPAbort;
  369.     pb->tcpStream                        = stream;
  370.     
  371.     pb->csParam.abort.userDataPtr        = userData;
  372.     
  373.     osErr = PBControl((ParmBlkPtr) pb, async);
  374.  
  375.     return osErr;
  376. }
  377.  
  378.  
  379. OSErr _TCPStatus (pb, stream, status, userData, ioCompletion, async)
  380.             TCPiopb                    *pb;
  381.             StreamPtr                stream;
  382.             struct TCPStatusPB        *status;
  383.             Ptr                        userData;
  384.             TCPIOCompletionProc        ioCompletion;
  385.             Boolean                    async;
  386. {
  387.  
  388.     auto    OSErr                    osErr;
  389.     
  390.     pb->ioCompletion                    = ioCompletion;
  391.     pb->ioCRefNum                        = gIPPDriverRefNum;
  392.     pb->csCode                            = TCPStatus;
  393.     pb->tcpStream                        = stream;
  394.     
  395.     pb->csParam.status.userDataPtr        = userData;
  396.     
  397.     osErr = PBControl((ParmBlkPtr) pb, async);
  398.     if (!async && (noErr == osErr)) {
  399.         *status                            = pb->csParam.status;    
  400.     }
  401.  
  402.     return osErr;
  403. }
  404.  
  405.  
  406. OSErr _TCPRelease (pb, stream, rcvBufPtr, rcvBufLen, ioCompletion, async)
  407.             TCPiopb                    *pb;
  408.             StreamPtr                stream;
  409.             Ptr                        *rcvBufPtr;
  410.             long                    *rcvBufLen;
  411.             TCPIOCompletionProc        ioCompletion;
  412.             Boolean                    async;
  413. {
  414.  
  415.     auto    OSErr                    osErr;
  416.     
  417.     pb->ioCompletion                    = ioCompletion;
  418.     pb->ioCRefNum                        = gIPPDriverRefNum;
  419.     pb->csCode                            = TCPRelease;
  420.     pb->tcpStream                        = stream;
  421.     
  422.     osErr = PBControl((ParmBlkPtr) pb, async);
  423.     if (!async && (noErr == osErr)) {
  424.         *rcvBufPtr                        = pb->csParam.create.rcvBuff;    
  425.         *rcvBufLen                        = pb->csParam.create.rcvBuffLen;    
  426.     }
  427.  
  428.     return osErr;
  429. }
  430.  
  431.